package com.xxxy.ljh.myapplication.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xxxy.ljh.myapplication.R;
import com.xxxy.ljh.myapplication.model.MemberApplication;

import java.util.List;

/**
 * 社团成员申请列表适配器
 */
public class MemberApplicationAdapter extends RecyclerView.Adapter<MemberApplicationAdapter.ViewHolder> {
    private List<MemberApplication> applicationList;
    private OnApplicationActionListener actionListener;

    public MemberApplicationAdapter(List<MemberApplication> applicationList, OnApplicationActionListener actionListener) {
        this.applicationList = applicationList;
        this.actionListener = actionListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_application_review, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MemberApplication application = applicationList.get(position);
        
        holder.tvUserName.setText(application.getUserName() != null ? application.getUserName() : "未知用户");
        holder.tvClubName.setText(application.getAssociationName() != null ? application.getAssociationName() : "未知社团");
        
        // 显示申请时间（如果可用）
        if (application.getApplyTime() != null && !application.getApplyTime().isEmpty()) {
            holder.tvApplyTime.setText("申请时间: " + application.getApplyTime());
        } else {
            holder.tvApplyTime.setText("申请时间: 未知");
        }
        
        // 设置按钮点击事件
        holder.btnApprove.setOnClickListener(v -> {
            if (actionListener != null) {
                actionListener.onApproveApplication(application);
            }
        });
        
        holder.btnReject.setOnClickListener(v -> {
            if (actionListener != null) {
                actionListener.onRejectApplication(application);
            }
        });
    }

    @Override
    public int getItemCount() {
        return applicationList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvUserName;
        TextView tvClubName;
        TextView tvApplyTime;
        Button btnApprove;
        Button btnReject;

        ViewHolder(View itemView) {
            super(itemView);
            tvUserName = itemView.findViewById(R.id.tv_user_name);
            tvClubName = itemView.findViewById(R.id.tv_club_name);
            tvApplyTime = itemView.findViewById(R.id.tv_apply_time);
            btnApprove = itemView.findViewById(R.id.btn_approve);
            btnReject = itemView.findViewById(R.id.btn_reject);
        }
    }
    
    public interface OnApplicationActionListener {
        void onApproveApplication(MemberApplication application);
        void onRejectApplication(MemberApplication application);
    }
}